import { Camera, Sprite, sys } from "cc";
import { Logger } from "./Logger";

export class ScreenShotHelper {

    private static camera: Camera;
    public static captureSp: Sprite;

    public static init(camera: Camera) {
        ScreenShotHelper.camera = camera
        ScreenShotHelper.camera.enabled = false;
        ScreenShotHelper.captureSp = camera.node.getChildByName("captureSp").getComponent(Sprite);
        ScreenShotHelper.captureSp.node.active = false;
    }

    public static captureNode(wantNode: Node, zoomRatio: number = 0) {
        // let tempX: number = wantNode.x;
        // let tempY: number = wantNode.y;
        // let designsize = cc.view.getDesignResolutionSize();
        // let camera: cc.Camera = ScreenShotHelper.camera
        // let texture = new cc.RenderTexture();
        // // 如果截图内容中不包含 Mask 组件，可以不用传递第三个参数
        // wantNode.setPosition(0, 0)
        // if (zoomRatio == 0) {
        //     if (wantNode.width > wantNode.height) {
        //         camera.zoomRatio = designsize.width / wantNode.width
        //         // Logger.log("width>height", camera.zoomRatio)
        //     } else {
        //         camera.zoomRatio = designsize.height / wantNode.height
        //         // Logger.log("width<height", camera.zoomRatio)
        //     }
        // } else {
        //     camera.zoomRatio = zoomRatio
        // }

        // texture.initWithSize(wantNode.width, wantNode.height);
        // camera.targetTexture = texture;
        // // 渲染一次摄像机，即更新一次内容到 RenderTexture 中
        // camera.enabled = true;
        // camera.render();
        // let textureData: Uint8Array = texture.readPixels();
        // wantNode.setPosition(tempX, tempY)
        // // Logger.log("captureNode====", camera.zoomRatio, textureData, wantNode.width, wantNode.height, texture.width, texture.height)
        // let picData: Uint8Array = this.filpYImage(textureData, texture.width, texture.height);
        // camera.enabled = false;
        // return picData;
    }

    // 搞不懂啊,  头都磕了,作揖却不愿意,非得自己翻转一下
    public static filpYImage(data, width, height) {
        // create the data array
        let picData = new Uint8Array(width * height * 4);
        let rowBytes = width * 4;
        for (let row = 0; row < height; row++) {
            let srow = height - 1 - row;
            let start = srow * width * 4;
            let reStart = row * width * 4;
            // save the piexls data
            for (let i = 0; i < rowBytes; i++) {
                picData[reStart + i] = data[start + i];
            }
        }
        return picData;
    }

    public static saveDataToFile(picData: Uint8Array, width: number, height: number, filePath: string) {
        // if (sys.isNative) {
        //     let success = jsb.saveImageData(picData, width, height, filePath)
        //     if (success) {
        //         Logger.log("saveDataToFile success, file: " + filePath);
        //     }
        // }
    }

    public static captureScreen(fullPath: string) {
        
        // let node = new cc.Node();
        // node.parent = cc.director.getScene();
        // node.width = cc.view.getVisibleSize().width;
        // node.height = cc.view.getVisibleSize().height;

        // Logger.log(`!!!!!! capture width:${node.width} node.height:${node.height}`);

        // //注意了，这里要讲节点的位置改为右上角，不然截图只有1/4
        // node.x = node.width / 2;
        // node.y = node.height / 2;

        // let camera = node.addComponent(cc.Camera);
        // // 设置你想要的截图内容的 cullingMask
        // camera.cullingMask = 0xffffffff;

        // // 新建一个 RenderTexture，并且设置 camera 的 targetTexture 为新建的 RenderTexture，这样 camera 的内容将会渲染到新建的 RenderTexture 中。
        // let texture = new cc.RenderTexture();
        // texture.initWithSize(node.width, node.height, cc.gfx.RB_FMT_S8);
        // camera.targetTexture = texture;

        // // 渲染一次摄像机，即更新一次内容到 RenderTexture 中
        // //node.parent.scaleY = -1;
        // // 截图默认是y轴反转的，渲染前需要将图像倒过来，渲染完倒回去
        // camera.render();
        // //node.parent.scaleY = 1;

        // // 这样我们就能从 RenderTexture 中获取到数据了
        // let data = texture.readPixels();
        // let { width } = texture;
        // let { height } = texture;
        // let picData = this.filpYImage(data, width, height);
        // //做个截屏的动作
        // //this.captureAction(node, width, height);

        // if (jsb.fileUtils.isFileExist(fullPath)) {
        //     jsb.fileUtils.removeFile(fullPath);
        // }
        // let success = jsb.saveImageData(picData, width, height, fullPath);
        // node.destroy();
        // if (success) {
        //     // eslint-disable-next-line no-console
        //     Logger.log('截屏成功，fullPath,width,height = ', fullPath, width, height);
        // }
        // else {
        //     Logger.error('截屏失败！');
        // }
    }
}
